package com.maxatime.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.maxatime.common.constant.BusinessContents;

import com.maxatime.pojo.entity.SysUser;
import com.maxatime.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.time.LocalDateTime;
import java.util.*;

/**
 * <p>
 * mybatis-plus练习增删改查
 * </p>
 *
 * @author Ssb
 * @since 2024-09-11
 */
@RestController
@RequestMapping("/sysUser")
public class SysUserController {

    @Autowired
    ISysUserService SysUserService;

    //list查询
    @RequestMapping("/quest1")
    public List<SysUser> quest(@RequestBody SysUser sysUser) {//mybatis-plus练习
        Map<String, Object> m = new HashMap<>();
        List<SysUser> list1 = SysUserService.list();
        System.out.println("sysUser.getUserName()======"+sysUser.getUserName());
        m.put(BusinessContents.CODE, "0000");
        m.put(BusinessContents.MSG, "交易成功");

        return list1;
    }
    //map查询
    @RequestMapping("/quest2")
    public Map<String, Object> quest2(@RequestBody SysUser sysUser) {//mybatis-plus练习
        Map<String, Object> m = new HashMap<>();
        List<SysUser> list1 = SysUserService.list();
        System.out.println("sysUser.getUserName()======"+sysUser.getUserName());
        m.put(BusinessContents.CODE, "0000");
        m.put(BusinessContents.MSG, "查询成功");
        m.put(BusinessContents.DATA, list1);
        return m;
    }
    //map分页查询
    @RequestMapping("/quest3")
    public Map<String, Object> quest3(@RequestBody SysUser sysUser,
                                      @RequestParam int page,
                                      @RequestParam int size) {//mybatis-plus练习
        Map<String, Object> m = new HashMap<>();
        Page<SysUser> page1 = new Page<>(page, size);//创建分页对象
        Page<SysUser> resultPage = SysUserService.page(page1);
        m.put(BusinessContents.CODE, "0000");
        m.put(BusinessContents.MSG, "查询成功");
        m.put(BusinessContents.DATA, resultPage.getRecords());// 返回当前页的用户列表
        m.put("total", resultPage.getTotal());// 返回总记录数
        return m;
    }
    //新增
    @RequestMapping("/save")
    public Map<String, Object> save(@RequestBody SysUser sysUser) {//mybatis-plus练习
        Map<String, Object> m = new HashMap<>();
        // 获取当前的LocalDateTime
        LocalDateTime d = LocalDateTime.now();
        sysUser.setCreateTime(d);
        sysUser.setLastLoginTime(d);
        sysUser.setDelFlag("0");
        sysUser.setStatus("0");
        sysUser.setCreateBy("admin");
        sysUser.setUpdateBy("admin");
        sysUser.setUpdateTime(d);
        SysUserService.save(sysUser);
        m.put(BusinessContents.CODE, "1111");
        m.put(BusinessContents.MSG, "新增成功");
        return m;
    }
    //删除
    @RequestMapping("/removeById")
    public Map<String, Object> removeById(@RequestBody String jsonStr) {//mybatis-plus练习
        Map<String, Object> m = new HashMap<>();
        JSONObject jsonObj = JSONObject.parseObject(jsonStr);
        int id = jsonObj.getInteger("id");
        SysUserService.removeById(id);
        m.put(BusinessContents.CODE, "3333");
        m.put(BusinessContents.MSG, "删除成功");
        return m;
    }
    //修改
    @RequestMapping("/updateById")
    public Map<String, Object> updateById(@RequestBody SysUser sysUser) {//mybatis-plus练习
        Map<String, Object> m = new HashMap<>();
        SysUserService.updateById(sysUser);
        m.put(BusinessContents.CODE, "4444");
        m.put(BusinessContents.MSG, "修改成功");
        // 调用 MyBatis-Plus 的 updateById 方法
//        boolean isUpdated = SysUserService.updateById(sysUser);

//        if (isUpdated) {
//            response.put("code", "4444");
//            response.put("msg", "修改成功");
//        } else {
//            response.put("code", "4445");
//            response.put("msg", "修改失败");
//        }

        return m;
    }

}
